package com.atguigu.day16.homework.h1;

import org.junit.Test;

import java.util.Arrays;

/**
 * @version 1.0
 * @Author 马冰
 * @Date 2024/11/19 20:45
 * @注释
 */
public class HomeWork03 {
    public static int binarySearch(String[] arr, String des) {

        int left = 0;
        int right = arr.length - 1;
        while (left <= right) {
            int mid = (right - left) / 2 + left;
            if (arr[mid].equals(des)) {
                return mid;
            } else if (arr[mid].compareTo(des) < 0) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }

        }
        return -1;
    }

    @Test
    public void test01() {

        int i = binarySearch(new String[]{"a", "b", "c", "d", "e", "f", "g", "h", "i"}, "h");
        System.out.println(i);


    }
}
